package com.pet.server.ext.alimama;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import com.pet.server.ext.ApiError;
import com.pet.server.ext.Logger;
import com.pet.server.ext.data.ALiMaMa;
import com.pet.server.ext.data.Constant;
import com.pet.server.ext.data.Image;
import com.pet.server.ext.service.ALiMaMaService;
import com.pet.server.ext.service.UserService;
import com.pet.server.ext.util.JsonParser;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.TbkUatmFavoritesGetRequest;
import com.taobao.api.request.TbkUatmFavoritesItemGetRequest;
import com.taobao.api.response.TbkUatmFavoritesGetResponse;
import com.taobao.api.response.TbkUatmFavoritesItemGetResponse;



public class AliMama {

	//public static final Logger logger = Logger.getLogger("topsdk");
	
	public static void main(String[] args) throws ApiException{	
		commodityList("http://gw.api.taobao.com/router/rest", "23333058", "2ff87185050ca9edc0702adf46380b1c");
	
	}
	
	
	/**
	 * 商品List
	 * @param url
	 * @param appkey
	 * @param secret
	 * @throws ApiException
	 */
	public static void commodityList(String url,String appkey,String secret) throws ApiException{
		TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
		TbkUatmFavoritesGetRequest req = new TbkUatmFavoritesGetRequest();
		req.setPageNo(1L);
		req.setPageSize(200L);
		req.setFields("favorites_title,favorites_id,type");
		req.setType(-1L);
		TbkUatmFavoritesGetResponse rsp = client.execute(req);
		System.out.println("qqq======="+rsp.getBody());
		ALiMaMaService articleService = new ALiMaMaService(); 
		//以上获取数据不用理会
		List<Map<String,String>> list=JsonParser.deal(rsp.getBody());//简单获取数据，没有用对象封装
				System.out.println("###以上是调试日志，下边是结果");
				for (Map<String,String> map:list) {
					System.out.print("================"+map.get("favorites_title"));
					String title=map.get("favorites_title");
					
					
					System.out.println(title);
					//System.out.println(title);
					
					String favorites_id=map.get("favorites_id");
					//System.out.println(title);
					String[] as = title.split("-");
					
					ALiMaMaService ams=new ALiMaMaService();
					
					   try {
						  String pet_cate=null;
						   
						   if(as[0].equals("狗")){
							   pet_cate="003002";
						   }else if(as[0].equals("猫")){
							   pet_cate="003001";
						   }else{
							   pet_cate="";
						   }
						   
						   
						   
						   
						   if(as.length>1){
							   Constant con = ams.findALiMaMa(as[1]);
							       
								   if(con!=null){	
									   
									  
									   
									   //删除该分类下的商品
									   articleService.delteProductData(pet_cate, con.getCode());
									   //重新插入数据库
									   commodityDetail(url, appkey, secret, Long.valueOf(favorites_id),con.getCode(),pet_cate,con.getLev()  );
								   }
						   }
						   
						
					} catch (ApiError e) {
						e.printStackTrace();
					}
					
					  
					
					
				}
		
		
	}
	/**
	 * 商品详情
	 * @param url
	 * @param appkey
	 * @param secret
	 * @throws ApiException
	 */
	public static void commodityDetail(String url,String appkey,String secret,Long favorites_id,String code,String pet_cate,Integer lev) throws ApiException{
		TaobaoClient client = new DefaultTaobaoClient(url, appkey, secret);
		
		TbkUatmFavoritesItemGetRequest req = new TbkUatmFavoritesItemGetRequest();
		req.setPlatform(2L);
		req.setPageSize(100L);
		req.setAdzoneId(52832143l);
		req.setUnid("3456");
		req.setFavoritesId( favorites_id);
		req.setPageNo(1L);
		req.setFields("num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,click_url,seller_id,volume,nick,shop_title,zk_final_price_wap,event_start_time,event_end_time,tk_rate,status,type");
		TbkUatmFavoritesItemGetResponse rsp = client.execute(req);
		//System.out.println("ttttt====="+rsp.getBody());
		
		List<Map<String,String>> list=JsonParser.dealDetil(rsp.getBody());//简单获取数据，没有用对象封装
		//System.out.println("###以上是调试日志，下边是结果");
		if(list!=null){
			//System.out.println("1111111="+list);
				for (Map<String,String> map:list) {
				ALiMaMaService articleService = new ALiMaMaService(); 
					ALiMaMa u=new ALiMaMa();
						u.setNum_iid(map.get("num_iid"));
						u.setProduct_name(map.get("title"));//商品名称
						u.setProduct_description(map.get("title"));//商品描述
						u.setSeller_id(map.get("seller_id"));//所属卖家
						u.setClick_url(map.get("click_url"));
						u.setIco_path(map.get("pict_url"));//产品图片
						u.setProduct_href(map.get("item_url"));//产品连接
						u.setProduct_cate(code);//产品分类
						if(pet_cate!=null){
							u.setPet_cate(pet_cate);//宠物分类
						}
						u.setCity(map.get("provcity"));
						u.setCreate_date(new Date());//创建时间
						u.setProduct_price(Double.valueOf(map.get("reserve_price")));//产品原价
						u.setSale_price(Double.valueOf(map.get("zk_final_price")));//产品促销价
						u.setState("0");
						u.setSort(lev);
						try {
							ALiMaMa aLmm = articleService.saveProductData(u);
							HashMap<String, String> map2 = JsonParser.toMap(map.get("small_images"));
							
							if(map2!=null){
								JSONArray jsonObject=JSONArray.fromObject(map2.get("string"));
								if(null!=jsonObject){
									for(Object img:jsonObject){
										UserService   us=new UserService();
										Image image=new Image();
										image.setOid(aLmm.getId());
										image.setType("product_data");
										image.setSrc(String.valueOf(img));
										us.saveImage(image);
									}
								}
							}
							
						} catch (ApiError e) {
							e.printStackTrace();
						}
						
						
					
					
				}
		}
		
	}
		
}
